package com.longrise.android.by.activity;

import android.content.Context;
import android.text.TextUtils;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.app.BYJKAppLike;
import com.longrise.common.base.BasePresenter;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.NetUtil;
import com.longrise.common.utils.PrintLog;

/**
 * Created by godliness on 2017/3/21.
 *
 */

public class BYWebPresenter extends BasePresenter {

    private static final String TAG = "BYWebPresenter";

    private BYWebView mView;
    /**
     * 根据BYContants.BY_EXTRA_PARAMS 决定加载相关web
     */
    private int mType;

    public BYWebPresenter(BYWebView view, int type) {
        mView = view;
        mType = type;
        mContext = BYJKAppLike.getAPP().getApplicationContext();
    }

    @Override
    protected void init() {
        mView.setTitle("保易网");

        request(mContext, mType);
    }

    public void request(Context cxt, int type) {
        if (!checkNetWorkEnable()) {
            mView.onRequestError();
            return;
        }
        mView.onRequestStart();
        int screenDp = 0;
        try {
            int topHeight = AppUtil.getStatusBar(cxt);
            int screenHeight = AppUtil.getScreenHeight();
            screenDp = AppUtil.px2dip(screenHeight - topHeight);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EntityBean entityBean = new EntityBean();
        entityBean.set("userid", UserInfor.getInstance().getUserid());
        entityBean.set("cardno", UserInfor.getInstance().getUsersfzh());
        entityBean.set("mobilephone", UserInfor.getInstance().getUserphone());
        entityBean.set("screenheight", screenDp - 45);
        entityBean.set("page", type);
        EntityBean reqBean = new EntityBean();
        reqBean.set("bean", entityBean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BY_URL, "app_isauthorization", reqBean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    clientParse((EntityBean) result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                mView.onRequestError();
            }

            @Override
            public void onFinished(String key, String service) {
            }

        });
    }

    private void clientParse(EntityBean result) {
        EntityBean bean = result.getBean("result");
        String url = bean.getString("href");
        PrintLog.e(TAG, "href : " + url);
        if (isEmptyUrl(url)) {
            mView.load(url);
        }
    }

    private boolean isEmptyUrl(String text) {
        if (TextUtils.isEmpty(text)) {
            mView.onRequestError();
            return false;
        } else {
            return true;
        }
    }

    protected boolean checkNetWorkEnable() {
        if (!NetUtil.checkNetEnable()) {
            return false;
        }
        return true;
    }
}
